package com.atguigu.headline.service.impl;

import com.atguigu.headline.dao.NewsHeadLineDao;
import com.atguigu.headline.dao.impl.NewsHeadlineDaoImpl;
import com.atguigu.headline.pojo.NewsHeadline;
import com.atguigu.headline.pojo.vo.HeadlineDetailVo;
import com.atguigu.headline.pojo.vo.HeadlinePageVo;
import com.atguigu.headline.pojo.vo.HeadlineQueryVo;
import com.atguigu.headline.service.NewsHeadlineService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsHeadlineServiceImpl  implements NewsHeadlineService {

    private NewsHeadLineDao newsHeadlineDao=new NewsHeadlineDaoImpl();

    @Override
    public Map pageQuery(HeadlineQueryVo headlineQueryVo) {
        int pageNum=headlineQueryVo.getPageNum();
        int pageSize=headlineQueryVo.getPageSize();
        int totalCountSize=newsHeadlineDao.queryTotalCount(headlineQueryVo);
        int totalpage=(totalCountSize%pageSize==0?totalCountSize/pageSize:totalCountSize/pageSize+1);
        List<HeadlinePageVo> list =newsHeadlineDao.queryPageList(headlineQueryVo);
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("totalCount",totalCountSize);
        map.put("totalPage",totalpage);
        map.put("pageData",list);
        return map;
    }

    @Override
    public HeadlineDetailVo showHeadlineDetail(int hid) {
        //1.浏览量+1
         newsHeadlineDao.increasePageViews(hid);
        //2查找
        return newsHeadlineDao.findHeadlineDetail(hid);
    }

    @Override
    public void insert(NewsHeadline newsHeadline) {
        newsHeadlineDao.insert(newsHeadline);
    }

    @Override
    public NewsHeadline query() {
        return newsHeadlineDao.query();
    }

    @Override
    public int update(NewsHeadline newsHeadline) {
        return newsHeadlineDao.update(newsHeadline);
    }

    @Override
    public int delte(int hid) {
        return newsHeadlineDao.delte(hid);
    }
}